<?php
class User_model extends CI_Model {
    //析构
    function __construct()
    {
        parent::__construct();
        $this->load->database();
    }
    //用户信息
    function userInfo(){
        $query = $this->db->get_where("users", array("Identifier"=>$this->session->userdata('openid')), 1);
        return $query->row_array();
    }
    //用户收藏
    function collected(){
        $this->db->select("u.id");
        $this->db->from("usercollected u");
        $this->db->join('content c', 'c.id=u.contentid AND u.from=1 AND c.status=1', 'left');
        $this->db->where("u.user='{$this->session->userdata('openid')}' AND c.id IS NOT NULL",NULL,FALSE);
        $query = $this->db->get();
        //echo $this->db->last_query();
        return $collectedNum = $query->num_rows();
    }
    //用户的登录时间变更
    function userchange(){
        //用户信息
        $user = $this->userInfo();
        if(!empty($user)){ //用户已经存在 update user
            if($user['LatestLogin']!=date("Y-m-d") && isset($user['Identifier']) && $this->session->userdata('openid')){//今天登录
                //连续登录次数
                if( $user['LatestLogin'] == date("Y-m-d",strtotime("-1 day")) ){
                    $ContinuousLoginTime = $user['ContinuousLoginTime']+1;
                }else{
                    $ContinuousLoginTime = 1;
                }
                //修改
                /* $this->db->set('LatestLogin', date("Y-m-d"));
                $this->db->set('ContinuousLoginTime', $ContinuousLoginTime);
                $this->db->set('yaoqingTime', 0);
                $this->db->set('zhaohuanTime', 0);
                $this->db->set('TotalLoginTime', 'TotalLoginTime+1', FALSE); */
                $data = array(
                		"LatestLogin"=>date("Y-m-d"),
                		"ContinuousLoginTime"=>$ContinuousLoginTime,
                		"yaoqingTime"=>0,
                		"zhaohuanTime"=>0,
                		"TotalLoginTime"=>$user['TotalLoginTime']+1
                );
                //用户来源
                if ($this->session->userdata('linkfrom')) {
                	if ($this->session->userdata('linkfrom')==='neihantu') {
                		$data = array_merge($data,array('comefrom'=>'neihantu'));
                	};
                }
                $this->db->where('Identifier', $this->session->userdata('openid'));
                $this->db->update('users',$data);
            }else{//今天已经登录
                //$todayIsLogin = 1;
            }
        }else{ //用户不存在   insert user
        	if(!isset($user['Identifier'])){
        		$data = array("Identifier" => $this->session->userdata('openid'),
        				"LatestCheckedIn" => date("Y-m-d",strtotime("-5 day")),
        				"LatestLogin" => date("Y-m-d"),
        				"GoldCoinNumber" => 0,
        				"ContinuousLoginTime" => 1,
        				"TotalLoginTime" => 1,
        				"register" => date("Y-m-d")
        		);
        		$this->db->insert('users', $data);
        	}
        }
        //更改用户的总刷新次数
        $this->db->set('Contents', 'Contents+1', FALSE);
        $this->db->where('Name', 'TotalView');
        $this->db->update('KeyValuePairs');
    }
}